package yiwise

import (
	"i-hea/utils/httplib"
	"path"
	"time"
)

type (
	ApiClient struct {
		appKey       string
		appSecret    string
		apiBaseUrl   string
		version      string
		tenantSig    string
		httpClient   *httplib.HttpClient
		tokenExpires time.Time
	}
)

const (
	YIWISE_DIALOG_PAGE_PARAM_TYPE_TEMPLATE = "TEMPLATE"
	YIWISE_DIALOG_PAGE_PARAM_TYPE_NORMAL   = "NORMAL"
)

//[DRAFT, PENDING_APPROVAL, REJECTED, APPROVED];
//at [Source: (PushbackInputStream); line: 1, column: 109] (through reference chain: com.yiwise.core.model.dialogflow.dto.DialogFlowFilterOptionDTO["status"

/*************一知话术通用接口********************/

// 接口信息
type Return struct {
	Code            int          `json:"code,omitempty"`
	Data            *interface{} `json:"data,omitempty"`
	RequestId       string       `json:"requestId,omitempty"`
	ResultMsg       string       `json:"resultMsg,omitempty"`
	ErrorStackTrace string       `json:"errorStackTrace,omitempty"`
	// 两种形式
	Msg string `json:"msg,omitempty"`
}

func (ret *Return) Ok() bool {
	return 200 == ret.Code
}

// 列表信息
type PageReturn struct {
	Number        int           `json:"number,omitempty"`
	PageSize      int           `json:"pageSize,omitempty"`
	TotalElements int           `json:"totalElements,omitempty"`
	Content       []interface{} `json:"content,omitempty"`
}

// 列表信息
type PageParam struct {
	PageNum  int `json:"pageNum,omitempty"`
	PageSize int `json:"pageSize,omitempty"`
}

/*************一知话术复制信息********************/

// 一知 话术录音上传参数
type UploadDialogFlowParam struct {
	DialogFlowId string `json:"dialogFlowId,omitempty" urlParam:"dialogFlowId"`
	Label        string `json:"label,omitempty" urlParam:"label"`
	FilePath     string `json:"file,omitempty" urlParam:"file"`
}

func (this *UploadDialogFlowParam) FileName() string {
	ext := path.Ext(this.FilePath)
	return this.Label + ext
}

/*************一知话术复制信息********************/

// 一知 话术复制接口参数
type CopyDialogFlowParam struct {
	Name                 string   `json:"name,omitempty" urlParam:"name"`
	BranchLevel          []string `json:"branchLevel,omitempty" urlParam:"branchLevel"`
	VadGateMute          int      `json:"VadGateMute,omitempty" urlParam:"vadGateMute"`
	MaxSentenceSilence   int      `json:"maxSentenceSilence,omitempty" urlParam:"maxSentenceSilence"`
	EnableAskService     bool     `json:"enableAskService,omitempty" urlParam:"enableAskService"`
	DialogFlowTemplateId int64    `json:"dialogFlowTemplateId,omitempty" urlParam:"dialogFlowTemplateId"`
	IntentLevelTagId     int      `json:"intentLevelTagId,omitempty" urlParam:"intentLevelTagId"`
	Description          string   `json:"description,omitempty" urlParam:"description"`
}

type DialogFlowCopyReturn struct {
	Return
	Data DialogFlowCopy `json:"data,omitempty"`
}

// ?
type DialogFlowCopy struct {
	Name         string `json:"name,omitempty"`
	DialogFlowId int    `json:"dialogFlowId,omitempty"`
}

/*************话术请求参数********************/

// 请求参数
type DialogFlowParam struct {
	DialogFlowId int `json:"dialogFlowId,omitempty" urlParam:"dialogFlowId"`
}

/*************一知机器人初始化********************/

// 初始化参数
type InitRobotParam struct {
	DialogFlowId int `json:"dialogFlowId,omitempty" urlParam:"dialogFlowId"`
	RobotCount   int `json:"robotCount,omitempty" urlParam:"robotCount"`
}

/*************一知话术列表信息********************/

//com.yiwise.core.model.enums.dialogflow.DialogFlowStatusEnum
//[DRAFT, PENDING_APPROVAL, REJECTED, APPROVED];
//at [Source: (PushbackInputStream); line: 1, column: 109] (through reference chain: com.yiwise.core.model.dialogflow.dto.DialogFlowFilterOptionDTO["status"

type DialogFlowPageParam struct {
	PageParam
	DialogFlowId string `json:"dialogFlowId,omitempty" urlParam:"dialogFlowId"` //"dialogFlowId",
	Name         string `json:"name,omitempty" urlParam:"name"`                 //"name",
	Industry     string `json:"industry,omitempty" urlParam:"industry"`         //"OTHER",
	SubIndustry  string `json:"subIndustry,omitempty" urlParam:"subIndustry"`   //"OTHER",
	Type         string `json:"type,omitempty" urlParam:"type"`                 //"TEMPLATE",
	Status       string `json:"status,omitempty" urlParam:"status"`             //"DRAFT"
}

// 话术接口信息
type DialogFlowReturn struct {
	Return
	Data *DialogFlowPageReturn `json:"data,omitempty"`
}

// 话术列表信息
type DialogFlowPageReturn struct {
	Number        int64        `json:"number"`
	PageSize      int64        `json:"pageSize"`
	TotalElements int64        `json:"totalElements"`
	Pages         int64        `json:"pages"`
	Content       []DialogFlow `json:"content,omitempty"`
}

type DialogFlow struct {
	Id                 int64    `json:"dialogFlowId,omitempty"`
	Name               string   `json:"name,omitempty"`
	Description        string   `json:"description,omitempty"`
	Status             string   `json:"status,omitempty"`
	Type               string   `json:"type,omitempty"`
	Industry           Industry `json:"industry,omitempty"`
	SubIndustry        Industry `json:"subIndustry,omitempty"`
	IntentLevelTagId   int      `json:"intentLevelTagId,omitempty"`
	IntentLevelTagName string   `json:"intentLevelTagName,omitempty"`
}

type Industry struct {
	Name string `json:"name,omitempty"`
	Desc string `json:"desc,omitempty"`
}

/*************一知话术详情接口********************/

type DialogFlowDetailReturn struct {
	Return
	Data *DialogFlowDetail `json:"data"`
}

// 一知话术详情接口
type DialogFlowDetail struct {
	DialogFlowId        int              `json:"dialogFlowId,omitempty"`
	Name                string           `json:"name,omitempty"`
	TotalRecordCount    int              `json:"totalRecordCount,omitempty"`
	CompleteRecordCount int              `json:"completeRecordCount,omitempty"`
	DialogFlowConfig    DialogFlowConfig `json:"dialogFlowConfig,omitempty"`
	DialogFlowSteps     []KnowledgeStep  `json:"dialogFlowSteps,omitempty"` //DialogFlowStepsBean 主流程
	RobotKnowledges     []RobotKnowledge `json:"robotKnowledges,omitempty"` //RobotKnowledge
	KnowledgeSteps      []KnowledgeStep  `json:"knowledgeSteps,omitempty"`  //KnowledgeStep 问答知识
}

func (this *DialogFlowConfig) DialogFlowConfigToNormalNode() []KnowledgeStep {
	var ret []KnowledgeStep
	addNode := func(title string, nodes []DialogFlowNode) {
		ret = append(ret, KnowledgeStep{
			Title:           title,
			DialogFlowNodes: nodes,
		})
	}
	if this.DialogFlowUserSayConfigs != nil {
		addNode("客户单句时长触发", this.DialogFlowUserSayConfigs)
	}
	if this.DialogFlowDeclineConfigs != nil {
		addNode("触发多次拒绝", this.DialogFlowDeclineConfigs)
	}
	if this.DialogFlowBusinessQAConfigs != nil {
		addNode("触发多个业务问题", this.DialogFlowBusinessQAConfigs)
	}

	return ret
}

func (this *DialogFlowDetail) RobotKnowlegeToNormalNode() []KnowledgeStep {
	var ret []KnowledgeStep
	node := this.RobotKnowledges
	if node != nil {
		for _, knowledge := range node {
			ret = append(ret, KnowledgeStep{
				Title:           knowledge.Title,
				DialogFlowNodes: knowledge.Answers,
			})

		}
	}
	return ret
}

// 话术个性化设置
type DialogFlowConfig struct {
	DialogFlowUserSayConfigs    []DialogFlowNode `json:"dialogFlowUserSayConfigs,omitempty"`    // 客户单句时长触发
	DialogFlowDeclineConfigs    []DialogFlowNode `json:"dialogFlowDeclineConfigs,omitempty"`    // 触发多次拒绝
	DialogFlowBusinessQAConfigs []DialogFlowNode `json:"dialogFlowBusinessQAConfigs,omitempty"` // 触发多个业务问题
}

// 机器人问答节点
type RobotKnowledge struct {
	Title   string           `json:"title,omitempty"`   // 问答知识标题
	Answers []DialogFlowNode `json:"answers,omitempty"` // 答案节点
}

//
// DialogFlowStepsBean KnowledgeStep
type KnowledgeStep struct {
	Title           string           `json:"title,omitempty"`                                    // 流程名称
	DialogFlowNodes []DialogFlowNode `json:"dialogFlowNodes,omitempty" json:"answers,omitempty"` // 流程节点
}

// 节点详细信息
type DialogFlowNode struct {
	Label           string `json:"label,omitempty"`   // 录音编号 上传录音时以此为文件名 下同
	Title           string `json:"title,omitempty"`   // 文本编号 当有变量时，一个文本会被拆分为多个录音
	Context         string `json:"context,omitempty"` // 录音文本 Part-X表示因变量被拆分成多个
	PrefixRecordUrl string `json:"prefixRecordUrl"`   //录音文件地址 此字段为空时未录音
}
